home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 7
/
Apprentice-Release7.iso
/
Source Code
/
Pascal
/
Applications
/
TCPExample
/
PNL Libraries
/
MyIPStrings.p
< prev
next >
Wrap
Text File
|
1996-11-09
|
2KB
|
104 lines
unit MyIPStrings;
interface
uses
Types, OpenTptInternet;
function StringToIPAddr (s: Str255; var addr: InetHost): boolean;
procedure IPAddrToString (ip: InetHost; var addrStr: Str255);
function IPAddrToStr (ip: InetHost): Str255;
procedure IPAddrPortToString (ip: InetHost; port: InetPort; var addrStr: Str255);
function IPAddrPortToStr (ip: InetHost; port: InetPort): Str255;
implementation
uses
TextUtils,
MyStrings;
function StringToIPAddr (s: Str255; var addr: InetHost): boolean;
var
good: boolean;
procedure Get1;
var
b: integer;
begin
if (length(s) = 0) | not (s[1] in ['0'..'9']) then begin
good := false;
end else begin
b := ord(s[1]) - 48;
s := TPcopy(s, 2, 255);
if (s <> '') & (s[1] in ['0'..'9']) then begin
b := b * 10 + ord(s[1]) - 48;
s := TPcopy(s, 2, 255);
end;
if (s <> '') & (s[1] in ['0'..'9']) then begin
b := b * 10 + ord(s[1]) - 48;
s := TPcopy(s, 2, 255);
end;
if (s <> '') & (s[1] = '.') then begin
s := TPcopy(s, 2, 255);
end;
if b > 255 then begin
good := false;
b := 0; { avoid overflow error? }
end;
addr := BOR(BSL(addr, 8), b);
end;
end;
begin
good := true;
addr := 0;
Get1;
Get1;
Get1;
Get1;
good := good & (s = '');
if not good then begin
addr := 0;
end;
StringToIPAddr := good;
end;
procedure IPAddrToString (ip: InetHost; var addrStr: Str255);
function GetByte(ip: InetHost; bits: integer): Str255;
var
t:Str255;
begin
NumToString(band(bsr(ip, bits), $00FF), t);
GetByte := t;
end;
begin
addrStr := GetByte(ip, 24);
addrStr := concat(addrStr, '.', GetByte(ip, 16));
addrStr := concat(addrStr, '.', GetByte(ip, 8));
addrStr := concat(addrStr, '.', GetByte(ip, 0));
end;
function IPAddrToStr (ip: InetHost): Str255;
var
s: Str255;
begin
IPAddrToString(ip, s);
IPAddrToStr := s;
end;
procedure IPAddrPortToString (ip: InetHost; port: InetPort; var addrStr: Str255);
var
ns:Str255;
begin
NumToString(port, ns);
addrStr := concat(IPAddrToStr(ip),':', ns);
end;
function IPAddrPortToStr (ip: InetHost; port: InetPort): Str255;
var
ns:Str255;
begin
NumToString(port, ns);
IPAddrPortToStr := concat(IPAddrToStr(ip),':', ns);
end;
end.